package com.wnhz.work.yz.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.AddressBean;

/**
 * ---发票
 */
public class InvoiceActivity extends BaseActivity implements View.OnClickListener {
    private final int FROM_INVOICE_TYPE = 642;
    private final int VALUE_TAX = 895;
    private TextView title;
    private TextView type_name;
    private TextView tv_texture;
    private View line;
    private RelativeLayout rl_company_name;
    private EditText et_company_name;

    private ImageView iv_head1, iv_head2;
    private ImageView iv_content1, iv_content2, iv_content3;

    private TextView tv_right;

    private String invoiceType = "个人";
    private String invoiceHeader = "个人";//发票抬头
    private String invoiceContent = "明细";//发票内容
    private String fullMessage;
    private TextView tv_noAddress_text;
    public static AppCompatActivity invoiceAct;

    private static final int CODE_ADDRESS = 111;
    private static final int CODE_INVOICE = 112;

    private View include_address, include_noAddress;
    private TextView tv_name, tv_phone, tv_address, tv_company_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invoice);

        invoiceAct = this;

        initData();
        initView();
    }


    private void initView() {
        tv_noAddress_text = (TextView) findViewById(R.id.tv_noAddress_text);
        tv_noAddress_text.setText("请填写发票寄往地址");
        tv_company_name = (TextView) findViewById(R.id.tv_company_name);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("发票");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.view_main_right_text);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("保存");
        tv_right.setTextColor(0xff008c99);
        findViewById(R.id.rl_choose_type).setOnClickListener(this);//选择类型
//        findViewById(R.id.rl_add_address).setOnClickListener(this);//选择地址
        type_name = (TextView) findViewById(R.id.tv_invoice_name);//类型名字
        tv_texture = (TextView) findViewById(R.id.tv_texture);//材质

        findViewById(R.id.rl_personage).setOnClickListener(this);//个人
        findViewById(R.id.rl_company).setOnClickListener(this);//单位
        iv_head1 = (ImageView) findViewById(R.id.iv_head1);
        iv_head2 = (ImageView) findViewById(R.id.iv_head2);

        line = findViewById(R.id.line);//线
        rl_company_name = (RelativeLayout) findViewById(R.id.rl_company_name);//单位名称布局
        et_company_name = (EditText) findViewById(R.id.et_company_name);//单位名称填写

        findViewById(R.id.rl_particular).setOnClickListener(this);//明细
        findViewById(R.id.rl_work).setOnClickListener(this);//办公用品
        findViewById(R.id.rl_cost).setOnClickListener(this);//耗材
        iv_content1 = (ImageView) findViewById(R.id.iv_content1);
        iv_content2 = (ImageView) findViewById(R.id.iv_content2);
        iv_content3 = (ImageView) findViewById(R.id.iv_content3);

        /**
         * 判断是否有地址选择显示隐藏的布局
         */
        include_address = findViewById(R.id.include_choose_address);//有地址
        include_noAddress = findViewById(R.id.include_choose_noAddress);//无收货地址

        include_address.setOnClickListener(this);
        include_noAddress.setOnClickListener(this);

        include_address.setVisibility(View.GONE);// TODO  暂时设为不可见

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_address = (TextView) findViewById(R.id.tv_address);

        et_company_name.setText(company);
        if (invoiceTitle.equals("个人")) {
            iv_head1.setImageResource(R.mipmap.ic_choose_li);
            iv_head2.setImageResource(R.mipmap.ic_choose_nor);
        } else {
            iv_head2.setImageResource(R.mipmap.ic_choose_li);
            iv_head1.setImageResource(R.mipmap.ic_choose_nor);
        }
        if (invoiceContent.equals("明细")) {

            iv_content1.setImageResource(R.mipmap.ic_choose_li);
            iv_content2.setImageResource(R.mipmap.ic_choose_nor);
            iv_content3.setImageResource(R.mipmap.ic_choose_nor);
        } else if (invoiceContent.equals("办公用品")) {
            iv_content2.setImageResource(R.mipmap.ic_choose_li);
            iv_content1.setImageResource(R.mipmap.ic_choose_nor);
            iv_content3.setImageResource(R.mipmap.ic_choose_nor);

        } else if (invoiceContent.equals("耗材")) {
            iv_content3.setImageResource(R.mipmap.ic_choose_li);
            iv_content1.setImageResource(R.mipmap.ic_choose_nor);
            iv_content2.setImageResource(R.mipmap.ic_choose_nor);

        }


    }

    private String company;

    private void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("invoice.txt",
                Activity.MODE_PRIVATE);
        fullMessage = sharedPreferences.getString("InvoiceResult", "");
        receiptAddrId = sharedPreferences.getString("receiptAddrId", "");
        invoiceType = sharedPreferences.getString("invoiceType", "个人");
        invoiceTitle = sharedPreferences.getString("invoiceTitle", "个人");
        company = sharedPreferences.getString("company", "");
        invoiceContent = sharedPreferences.getString("receiptType", "明细");
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.btn_main_right:
                String text = et_company_name.getText().toString();
                if (null != text && !"".equals(text) && !"".equals(receiptAddrId) && null != receiptAddrId) {


                    invoiceHeader = text;
                    fullMessage = invoiceType + "-" + invoiceContent;
                    getResultAndFinishActivity(fullMessage, receiptAddrId, "1", invoiceType, text, invoiceContent);
                } else if ("".equals(text)) {
                    MyToast("请填写名称");
                } else if ("".equals(receiptAddrId) || tv_noAddress_text.getText().equals("请填写发票寄往地址")) {
                    MyToast("请选择收发票地址");
                } else if (null == receiptAddrId) {
                    MyToast("请选择收发票地址");
                }
                break;

            //选择类型
            case R.id.rl_choose_type:
                intent = new Intent(this, InvoiceTypeActivity.class);
                startActivityForResult(intent, FROM_INVOICE_TYPE);
                break;

            //有地址，前去修改
            case R.id.include_choose_address:
                intent = new Intent(this, ChooseAddressActivity.class);
                startActivityForResult(intent, CODE_ADDRESS);
                break;

            //无地址，添加收货地址
            case R.id.include_choose_noAddress:
                intent = new Intent(this, ChooseAddressActivity.class);
                startActivityForResult(intent, CODE_ADDRESS);
                break;

            //个人
            case R.id.rl_personage:
                invoiceType = "个人";
                invoiceHeader = "个人";

                iv_head1.setImageResource(R.mipmap.ic_choose_li);
                iv_head2.setImageResource(R.mipmap.ic_choose_nor);
//                line.setVisibility(View.GONE);
//                rl_company_name.setVisibility(View.GONE);
                tv_company_name.setText("个人名称");
                break;

            //单位
            case R.id.rl_company:
                invoiceType = "单位";
                invoiceHeader = "";
                tv_company_name.setText("单位名称");
                iv_head1.setImageResource(R.mipmap.ic_choose_nor);
                iv_head2.setImageResource(R.mipmap.ic_choose_li);
//                line.setVisibility(View.VISIBLE);
//                rl_company_name.setVisibility(View.VISIBLE);
                break;

            //明细
            case R.id.rl_particular:
                invoiceContent = "明细";

                iv_content1.setImageResource(R.mipmap.ic_choose_li);
                iv_content2.setImageResource(R.mipmap.ic_choose_nor);
                iv_content3.setImageResource(R.mipmap.ic_choose_nor);
                break;

            //办公用品
            case R.id.rl_work:
                invoiceContent = "办公用品";

                iv_content1.setImageResource(R.mipmap.ic_choose_nor);
                iv_content2.setImageResource(R.mipmap.ic_choose_li);
                iv_content3.setImageResource(R.mipmap.ic_choose_nor);
                break;

            //耗材
            case R.id.rl_cost:
                invoiceContent = "耗材";

                iv_content1.setImageResource(R.mipmap.ic_choose_nor);
                iv_content2.setImageResource(R.mipmap.ic_choose_nor);
                iv_content3.setImageResource(R.mipmap.ic_choose_li);
                break;
            default:
                break;
        }
    }

    private String typeName;
    private String receiptAddrId;//发票收货地址id
    private String invoiceTitle;
    private String taxpayerId;
    private String bank;
    private String bankAccount;
    private String address;
    private String phone;
    private String type1;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FROM_INVOICE_TYPE:
                if (resultCode == RESULT_OK) {
                    typeName = data.getExtras().getString("result");
                    invoiceTitle = data.getExtras().getString("invoiceTitle");
                    taxpayerId = data.getExtras().getString("taxpayerId");
                    bank = data.getExtras().getString("bank");
                    bankAccount = data.getExtras().getString("bankAccount");
                    address = data.getExtras().getString("address");
                    phone = data.getExtras().getString("phone");
                    type1 = data.getExtras().getString("type1");
                    //type_name.setText(typeName);

                    getResultAndFinishActivity1(typeName, invoiceTitle, taxpayerId, bank, bankAccount, address, phone, type1);
                }
                break;

            //地址回调
            case CODE_ADDRESS:
                if (resultCode == RESULT_OK) {
                    if (null != data) {
                        AddressBean addressBean = (AddressBean) data.getSerializableExtra("addressBean");
                        include_address.setVisibility(View.VISIBLE);
                        include_noAddress.setVisibility(View.GONE);
                        String province = addressBean.getProvince();
                        String city = addressBean.getCity();
                        String district = addressBean.getDistrict();
                        String place = addressBean.getPlace();
                        String addr = province + city + district + place;
                        tv_name.setText(addressBean.getConsignee());
                        tv_phone.setText(addressBean.getTelephone());
                        tv_address.setText(addr);

                        receiptAddrId = addressBean.getAddress_id();
                    }
                }
                break;

            default:
                break;
        }
    }


    public void getResultAndFinishActivity(String name, String receiptAddrId, String invoiceType, String invoiceHeader, String company, String receiptType) {
        //实例化SharedPreferences对象（第一步）
        SharedPreferences mySharedPreferences = getSharedPreferences("invoice.txt",
                Activity.MODE_PRIVATE);
        //实例化SharedPreferences.Editor对象（第二步）
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        //用putString的方法保存数据
        editor.putString("InvoiceResult", name);
//        editor.putString("receiptAddrId", receiptAddrId);
        editor.putString("invoiceType", invoiceHeader);
        editor.putString("invoiceTitle", invoiceHeader);
        editor.putString("company", company);
        editor.putString("receiptType", receiptType);
        //提交当前数据
        editor.commit();
        Log.e("--", name + "+" + receiptAddrId + "+" + invoiceType + "+" + invoiceHeader + "+" + company + "+" + receiptType);

        Intent intent = new Intent();
        intent.putExtra("InvoiceResult", name);
        intent.putExtra("receiptAddrId", receiptAddrId);
        intent.putExtra("invoiceType", invoiceType);
        intent.putExtra("invoiceTitle", invoiceHeader);
        intent.putExtra("company", company);
        intent.putExtra("receiptType", receiptType);
        intent.putExtra("type", "1");
        setResult(RESULT_OK, intent);
        finish();

    }

    public void getResultAndFinishActivity1(String name, String invoiceTitle, String taxpayerId,
                                            String bank, String bankAccount, String address,
                                            String phone, String type1) {
        Intent intent = new Intent();
        intent.putExtra("InvoiceResult", name);
        intent.putExtra("invoiceTitle", invoiceTitle);
        intent.putExtra("taxpayerId", taxpayerId);
        intent.putExtra("bank", bank);
        intent.putExtra("bankAccount", bankAccount);
        intent.putExtra("address", address);
        intent.putExtra("phone", phone);
        intent.putExtra("type1", type1);
        SharedPreferences mySharedPreferences = getSharedPreferences("invoice.txt",
                Activity.MODE_PRIVATE);
        //实例化SharedPreferences.Editor对象（第二步）
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        //用putString的方法保存数据
        editor.putString("InvoiceResult", name);
        editor.putString("invoiceTitle", invoiceTitle);
        editor.putString("taxpayerId", taxpayerId);
        editor.putString("bank", bank);
        editor.putString("bankAccount", bankAccount);
        editor.putString("address", address);
        editor.putString("phone", phone);
        editor.putString("type1", type1);
        //提交当前数据
        editor.commit();
        setResult(VALUE_TAX, intent);
        finish();

    }
}

